package org.wishwork.api.product.combine.upload;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.handler.HandleHelper;
import com.alibaba.fastjson.JSONObject;
import org.wishwork.api.Api;
import org.wishwork.api.product.combine.upload.request.CombineProductUploadRequest;
import org.wishwork.api.product.combine.upload.request.Item;
import org.wishwork.context.Context;
import org.wishwork.helper.HttpHelper;

import javax.print.attribute.HashAttributeSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 组合装商品上传
 * @author: WangYX
 * @create: 2022-04-11 15:39
 * @Version: 1.0.0
 **/

public class CombineProductUploadApi extends Api<CombineProductUploadResponse, CombineProductUploadRequest> {

    private Context context;

    public CombineProductUploadApi(Context context) {
        this.context = context;
    }

    @Override
    protected String query(CombineProductUploadRequest combineProductUploadRequest) throws Exception {
        String url = "/open/jushuitan/item/combinesku/upload";

        Map<String, Object> params = new HashMap<>();
        params.put("sku_id", combineProductUploadRequest.getSkuId());
        params.put("i_id", combineProductUploadRequest.getIId());
        params.put("name", combineProductUploadRequest.getName());

        if (StrUtil.isNotEmpty(combineProductUploadRequest.getBrand())) {
            params.put("brand", combineProductUploadRequest.getBrand());
        }
        if (ObjectUtil.isNotEmpty(combineProductUploadRequest.getCostPrice())) {
            params.put("cost_price", combineProductUploadRequest.getCostPrice());
        }
        if (StrUtil.isNotEmpty(combineProductUploadRequest.getPropertiesValue())) {
            params.put("properties_value", combineProductUploadRequest.getPropertiesValue());
        }
        if (StrUtil.isNotEmpty(combineProductUploadRequest.getPic())) {
            params.put("pic", combineProductUploadRequest.getPic());
        }
        if (StrUtil.isNotEmpty(combineProductUploadRequest.getShortName())) {
            params.put("short_name", combineProductUploadRequest.getShortName());
        }
        if (StrUtil.isNotEmpty(combineProductUploadRequest.getRemark())) {
            params.put("remark", combineProductUploadRequest.getRemark());
        }
        if (combineProductUploadRequest.getItems() != null && combineProductUploadRequest.getItems().size() > 0) {
            List<Map<String, Object>> list = new ArrayList<>();
            for (Item item : combineProductUploadRequest.getItems()) {
                Map<String, Object> map = new HashMap<>();
                map.put("src_sku_id", item.getSrcSkuId());
                map.put("qty", item.getQty());
                map.put("sale_price", item.getSalePrice());
                list.add(map);
            }
            params.put("items", list);

        }
        return HttpHelper.sendRequest(params, url);
    }

    @Override
    protected CombineProductUploadResponse process(JSONObject data, CombineProductUploadRequest combineProductUploadRequest) throws Exception {
        return null;
    }
}
